#!/usr/bin/php
<?php
// set up the Mongodb connection
$mongo = new Mongo();

// the database itself.
$db = $mongo->mugshots;

// the collection (nee table)
$collection = $db->shots;

$db->drop();

// parse input args
// NOTE: should be a zend getopts
$limit = (isset($argv[1]) ? $argv[1]: false);

// iterate
foreach(new DirectoryIterator("/mnt/mugshots/mugshots_pinellas/html/") as $i => $f)
{
	// strip out everything but the 'docket' id.
	preg_match("/(^\d+)\.html$/", basename($f->getFilename()), $file);

	if($limit && $i == $limit)
	  	die("Hit limit, $limit, dying\n");

	// appropriate the HTML data
	$details = file_get_contents($f->getPathname());

	// see if we already have this data in the mongo collection. if so, skip over.
	if($collection->find(array('docket' => $file[1]))->count() > 0)
	{
		print(($i%1000==0) ? "\n".$i."\n" : "~");
		continue;
	}

	if($record = @parse($details))
	{
		// if on fs, use image from fs, else fetch from web, store to fs
		if(!$image = @file_get_contents("/mnt/mugshots/mugshots_pinellas/images/{$file[1]}.jpg"))
		{
			// get from sheriff w/context 
			$c = stream_context_create(array(
				'http' => array('timeout' => 3)
			));
			// use stream context so I can timeout if request takes longer than 3 seconds
			$image = file_get_contents("http://www.pcsoweb.com/InmateBooking/RenderImage.aspx?size=full&id={$file[1]}",0, $c);

			// Be nice, don't overload the sheriff, they're union labor!
			sleep(5);

			file_put_contents("/mnt/mugshots/mugshots_pinellas/images/{$file[1]}.jpg", $image);
		}

		// use imagemagick to shrink the image

		try {

			$im = new Imagick;
			$im->readImageBlob($image);
			$im->setCompression(Imagick::COMPRESSION_JPEG);
			$im->setCompressionQuality(20);
			list($h, $w) = array_values($im->getImageGeometry());
			$im->thumbnailImage($h *= .50, $w *= .50);
			$im->stripImage();


			// store the binary image data
			$record['image'] = new MongoBinData($im->getImageBlob());


			// scoring mechanism
			$record['score'] = 0;
			$record['votes'] = 0;
			$record['seq'] = ++$i;
			$record['comments'] = array();
		  
			$collection->insert($record);
			print(($i%1000==0) ? "\n".$i."\n" : ".");

		} catch (ImagickException $e) {

			print("*");

		}

	} else {

		print("x");

	}
}

// data import complete, set up the indexes
// NOTE: This doesn't seem to work, so, indexes should be created by hand
// ex. db.shots.ensureIndex({arrest_date:1}), db.ensureIndex({docket:1})
// $collection->ensureIndex(array('arrest_date' => 1));

///////////////

/**
 * use regular expression to pull out pertinent chunks of data from the HTML page we've downloaded
 * 
 * @param string $details the HTML
 * 
 * @return mixed array|boolean false
 */
function parse($details)
{
  $record = array();

  preg_match("/(?:<span id=\"lblName\">)(?<name>.*)(?:<\/span>)/i", $details, $data);
  $record['name'] = $data['name'];

  preg_match("/(?:<span id=\"lblDocketNumber\">)(?<docket>.*)(?:<\/span>)/i", $details, $data);
  $record['docket'] = $data['docket'];

  preg_match("/(?:<span id=\"lblArrestDate\">)(?<arrest_date>.*)(?:<\/span>)/i", $details, $data);
  $record['arrest_date'] = new MongoDate(strtotime($data['arrest_date']));

  preg_match("/(?:<span id=\"lblRace\">)(?<race>.*)(?:<\/span>)/i", $details, $data);
  $record['race'] = $data['race'];

  preg_match("/(?:<span id=\"lblSex\">)(?<sex>.*)(?:<\/span>)/i", $details, $data);
  $record['sex'] = $data['sex'];

  preg_match("/(?:<span id=\"lblDOB1\">)(?<dob>.*)(?:<\/span>)/i", $details, $data);
  $record['dob'] = new MongoDate(strtotime($data['dob']));

  preg_match("/(?:<span id=\"lblAddress\">)(?<address>.*)(?:<\/span>)/i", $details, $data);
  $record['address'] = $data['address'];

  preg_match("/(?:<span id=\"lblCity\">)(?<city>.*)(?:<\/span>)/i", $details, $data);
  $record['city'] = $data['city'];

  preg_match("/(?:<span id=\"lblState\">)(?<state>.*)(?:<\/span>)/i", $details, $data);
  $record['state'] = $data['state'];

  preg_match("/(?:<span id=\"lblZipCode\">)(?<zip>.*)(?:<\/span>)/i", $details, $data);
  $record['zip'] = $data['zip'];

  preg_match_all("/<td.*>Offense.*<\/td><td.*\">(?<offenses>.*)(?=<\/td>)/", $details, $data);
  $record['offenses'] = $data['offenses'];

  // return false if no name
  return (isset($record['name']) && $record['name'] != '') ? $record : false;

}




